/// Creator:      gaoruishan
/// CreatDate:    2025-07-21
/// Description:  医嘱相关
Class Nur.MNISV3.Service.Order Extends %RegisteredObject [ Not ProcedureBlock ]
{

// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Order).GetOrdersV4("OrderExecute","search","","CQSYD","2025-09-07","00:00","2025-09-09","11:59","1","20","","{""single"":""1"",""select"":""0"",""activity"":""UniversalActivity"",""scan"":""0"",""multiple"":""0"",""sheet"":""[CQSYD, CQSYD]"",""check"":""0"",""manual"":""0"",""panel"":"""",""open"":""0"",""switch"":""0"",""status"":""""}","8","23","3","2","20665"))

// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Order).GetOrdersV4("OrderExecute","search","","CQSYD","2025-09-07","00:00","2025-09-09","11:59","1","20","","{""single"":""1"",""select"":""0"",""activity"":""UniversalActivity"",""scan"":""0"",""multiple"":""0"",""sheet"":""[CQSYD, CQSYD]"",""check"":""0"",""manual"":""0"",""panel"":"""",""open"":""0"",""switch"":""0"",""status"":""""}","8","23","3","2","20665")

/// 查询医嘱
/// "motion": "search" 是查询
/// "extras": "{\"single\":\"1\",\"select\":\"0\",\"activity\":\"UniversalActivity\",\"scan\":\"0\",\"multiple\":\"0\",\"sheet\":\"[DefaultSee, ]\",\"check\":\"0\",\"manual\":\"0\",\"panel\":\"\",\"open\":\"0\",\"switch\":\"0\",\"status\":\"\"}",
/// "[{\"barcode\":\"0000000689\",\"entity\":\"patient\",\"manual\":\"1\",\"result\":\"1217\",\"scan\":\"0\"}]",
/// entities 实体:  "barcode": "0000000598" 登记号 或瓶贴 ; "entity": "patient" 患者 或医嘱order
///               "manual": "0" 是否手动 "scan": "1" 是否扫码 ; "result": "1708" 结果是就诊号
/// s ^TEMP("GetOrdersV4")=$lb(module, motion, entities, sheetCode, startDate, startTime, endDate, endTime, pageNo, limitNo, screenParts, extras, locId, groupId, wardId, hospitalId, userId)behavior 行为: "request" 是请求; "open"或"close"是打开或关闭panels面板 "check"或"uncheck" 是由onCheckOrderV4发起
/// request  请求名称 "GetOrdersV4" 或"GetOrderTasksV4" 或"GetOrdersV4&GetOrderTasksV4" 或"ExecOrSeeOrderV4"
/// dialog   对话框  格式固定看CreateDialog方法
/// toast    通用toast提示(任何请求方法都可添加)
/// w ##class(Nur.MNISV3.Service.Order).GetOrdersV4("OrderExecute","search","","","2025-08-07","00:00","2025-08-09","11:59","1","20","","{""single"":""1"",""select"":""0"",""activity"":""UniversalActivity"",""scan"":""0"",""multiple"":""0"",""sheet"":""[]"",""check"":""0"",""manual"":""0"",""panel"":"""",""open"":""0"",""switch"":""0"",""status"":""""}","8","23","3","2","20665")
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Order).GetOrdersV4("OrderExecute","search","","DefaultSee","2025-09-08","00:00","2025-09-10","11:59","1","20","","{""single"":""1"",""select"":""0"",""activity"":""UniversalActivity"",""scan"":""0"",""multiple"":""0"",""sheet"":""[DefaultSee, DefaultSee]"",""check"":""0"",""manual"":""0"",""panel"":"""",""open"":""0"",""switch"":""0"",""status"":""""}","8","23","3","2","20665"))
ClassMethod GetOrdersV4(module, motion, entities, sheetCode, startDate, startTime, endDate, endTime, pageNo, limitNo, screenParts, extras, locId, groupId, wardId, hospitalId, userId) As %String
{
    n (module,motion,entities,sheetCode,startDate,startTime,endDate,endTime,pageNo,limitNo,screenParts,extras,locId, groupId, wardId,hospitalId,userId,%session)  // 必须加%session 调用别人代码会用到session
    s $zt="errGetOrdersV4"
    s ^GRS("GetOrdersV4",$g(userId)_"-")=$lb(module,motion,entities,sheetCode,startDate,startTime,endDate,endTime,pageNo,limitNo,screenParts,extras,locId, groupId, wardId,hospitalId,userId)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    //取筛选条件
    s excutedOrderFlag="false", bedList="",regNo=""
    s tmpScreen=""
    //解析screenParts 
    d ..handleScreenParts(screenParts,.excutedOrderFlag)
    w excutedOrderFlag,!
    //传递barcode实体
    i $g(entities)'="" {
        s entitiesList=##class(Nur.MNISV3.Common.JSON).Decode(entities) 
        f idx=1:1:entitiesList.Count(){
            s tEntity=entitiesList.GetAt(idx).GetAt("entity")
            //患者--腕带
            i $g(tEntity)="patient" s regNo=entitiesList.GetAt(idx).GetAt("barcode")
        }
    }
    //默认单据code
    s defaultSheetStr=..GetDefaultSheetStr(groupId,hospitalId,locId)
    s:(defaultSheetStr'="")&(sheetCode="") sheetCode=$P(defaultSheetStr,"@",2)
    s:sheetCode="" sheetCode="DefaultSee"
    
    s ordSheets=..GetSheetsListOfSSGroup(groupId,hospitalId,locId)
   
    //s sheetCode="CQSYD"
     ///默认参数
    s PDAParam=##class(OrderParam).%New()
    s PDAParam.PDAStart=(pageNo-1)*limitNo+1
    s PDAParam.PDALimit= pageNo*limitNo
    s PDAParam.PDAScreenings=screenParts
    s PDAParam.PDAMulti=0 //是否多个单据
    s PDAParam.PDAGetOrder=0 //0即返回obj对象 

    s disposeStateInfo= ##class(Nur.MNISV3.Service.QueryOrder).GetDisposeStateInfo(hospitalId)
    s episodeIDStr=""
    //取设置所管床，未设置默认全部，有传登记regNo 则只返回regNo
    d ##class(Nur.MNISV3.Service.Bed).GetCurWardManagedBedTemp(wardId,userId,.tmp,regNo)
    zw tmp
    s patOrderList=##class(%ListOfDataTypes).%New()
    s bedSeqNo=""
    f
    {
        s bedSeqNo=$o(tmp(bedSeqNo)) q:bedSeqNo=""
        s cbedY=""  
        f  
        {
            s cbedY=$O(tmp(bedSeqNo,cbedY)) q:cbedY=""
            s cbedX=""  
            f  
            {
                s cbedX=$O(tmp(bedSeqNo,cbedY,cbedX)) 
                q:cbedX=""
                s cbedCode=""  
                f  
                {
                    s cbedCode=$O(tmp(bedSeqNo,cbedY,cbedX,cbedCode)) 
                    q:cbedCode=""
                    s episodeId=$p(tmp(bedSeqNo,cbedY,cbedX,cbedCode),"^",1)
                    s patOrds=##class(Nur.MNISV3.Service.QueryOrder).GetOrders(PDAParam, episodeId, sheetCode, groupId, startDate, startTime, endDate, endTime, hospitalId, wardId, locId, "true", "false",  "A", excutedOrderFlag, "","false","false","false","false","")
                    w ##class(Nur.JSON).Encode(patOrds),!
                    //通过获取的Id再取医嘱基本信息，再组装数据
                    s orderInfoMain=..ConvertPdaPatOrds(sheetCode,patOrds,disposeStateInfo)
                    s patOrdeInfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo("",episodeId)
                    d patOrdeInfo.SetAt(orderInfoMain,"orders")
                    d patOrderList.Insert(patOrdeInfo)
                }
            }
        }
    }
    s screenParts=##class(Nur.MNISV3.Service.Config).GetOrderFilterObject(locId,groupId)
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt(patOrderList,"patOrds")
    d json.SetAt($g(ordSheets),"ordSheets")
    d json.SetAt(sheetCode,"sheetDefCode")
    s curDateTime=$zd(+$h,3)_" "_$zt($p($h,",",2))
    d json.SetAt($g(curDateTime),"curDateTime")
    d json.SetAt($g(screenParts),"screenParts")
    q json
    //q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errGetOrdersV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100010","-1")
}

/// 查询医嘱数量（任务）
/// {"limitNo":"20","package":"com.dhcc.nursepro4","motion":"search","sheetCode":"DefaultSee","endDate":"2025-08-08","module":"OrderExecute","groupId":"23","extras":"{\"single\":\"1\",\"select\":\"0\",\"activity\":\"UniversalActivity\",\"scan\":\"0\",\"multiple\":\"0\",\"sheet\":\"[DefaultSee, DefaultSee]\",\"check\":\"0\",\"manual\":\"0\",\"panel\":\"\",\"open\":\"0\",\"switch\":\"0\",\"status\":\"\"}","wardId":"3","secret":"aU1lZGljYWwtTnVyc2UtUERBNC4wT3JkZXJzRnJhZ21lbnRkYzMwYThhMzE5MjcyNWFkXkRUNTBI\nMTc1NDYyMDI5NTQyOA==\n","logonInfo":"{\"hospitalId\":\"2\",\"groupId\":\"23\",\"wardId\":\"3\",\"userId\":\"20665\",\"locId\":\"8\"}","deviceId":"dc30a8a3192725ad^DT50H^192.168.1.101,","userId":"20665","fragment"
/// w ##class(Nur.MNISV3.Service.Order).GetOrderTasksV4("OrderExecute","search","","DefaultSee","2025-08-07","00:00","2025-08-09","11:59","1","20","","{""single"":""1"",""select"":""0"",""activity"":""UniversalActivity"",""scan"":""0"",""multiple"":""0"",""sheet"":""[DefaultSee, DefaultSee]"",""check"":""0"",""manual"":""0"",""panel"":"""",""open"":""0"",""switch"":""0"",""status"":""""}","8","23","3","2","20665")
ClassMethod GetOrderTasksV4(module, motion, entities, sheetCode, startDate, startTime, endDate, endTime, pageNo, limitNo, screenParts, extras, locId, groupId, wardId, hospitalId, userId) As %String
{
    n (module,motion,entities,sheetCode,startDate,startTime,endDate,endTime,pageNo,limitNo,screenParts,extras,locId, groupId, wardId,hospitalId,userId,%session)  // 必须加%session 调用别人代码会用到session
    s $zt="errGetOrderTasksV4"
    s ^GRS("GetOrderTasksV4")=$lb(module,motion,entities,sheetCode,startDate,startTime,endDate,endTime,pageNo,limitNo,screenParts,extras,locId, groupId, wardId,hospitalId,userId)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s sheetCode=$replace(sheetCode,$c(0),"")
    //单据列表
    s ordSheets=..GetSheetsListOfSSGroup(groupId,hospitalId,locId)
    //取设置所管床，未设置默认全部
    s wardBeds= ##class(Nur.MNISV3.Service.Bed).GetCurWardManagedBedTemp(wardId,userId)
    ///默认参数
    s PDAParam=##class(OrderParam).%New()
    s PDAParam.PDAStart=1 ;(pageNo-1)*limitNo+1
    s PDAParam.PDALimit= 999999 ;pageNo*limitNo
    s PDAParam.PDAScreenings=screenParts
    s PDAParam.PDAMulti=0
    s PDAParam.PDAGetOrder=0 ;返回obj对象,不进行json序列化

    s excutedOrderFlag="false"
    //解析screenParts 
    d ..handleScreenParts(screenParts,.excutedOrderFlag)
    s ordTasks=##class(%ListOfDataTypes).%New()  
    s statTasks=##class(%ListOfDataTypes).%New() 
    k tempPatInfos //临时记录病人
    f ind=1:1:ordSheets.Count(){
        s sheetObj=ordSheets.GetAt(ind)
        s tmpSheetCode=sheetObj.GetAt("sheetCode")
        s tmpsheetName=sheetObj.GetAt("sheetName")
        i $g(sheetCode)'="" { //不为空 只查当前的
            continue:$g(sheetCode)'=tmpSheetCode
        }
        s ordNum=0
        s ordPats=##class(%ListOfDataTypes).%New()  
        f j=1:1:wardBeds.Count(){
            s bedObj=wardBeds.GetAt(j)
            s episodeId=bedObj.GetAt("episodeId")
            i $g(tempPatInfos(episodeId))'="" { //取缓存的
                s patInfo=$g(tempPatInfos(episodeId))
            }else{  
                s patInfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo("",episodeId)
                s tempPatInfos(episodeId)=patInfo
            }
            s patOrds=##class(Nur.MNISV3.Service.QueryOrder).GetOrders(PDAParam, episodeId, tmpSheetCode, groupId, startDate, startTime, endDate, endTime, hospitalId, wardId, locId, "true", "false",  "A", excutedOrderFlag, "","false","false","false","false","")
            s ordNumPat=patOrds.Data("orders").Count()
            s ordNum=ordNum+ordNumPat 
            d patInfo.SetAt($g(ordNumPat),"ordNum")
            d ordPats.Insert(patInfo)
        }
        s ordTaskObj=##class(%ArrayOfDataTypes).%New()
        d ordTaskObj.SetAt(tmpSheetCode,"sheetCode")
        d ordTaskObj.SetAt(tmpsheetName,"sheetName")
        i $g(ordNum)="0" s ordNum="" // 0不显示
        d ordTaskObj.SetAt($g(ordNum),"ordNum")
        d ordTaskObj.SetAt(ordPats,"ordPats")
        d ordTasks.Insert(ordTaskObj)
    }
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(ordSheets),"ordSheets")
    d json.SetAt($g(statTasks),"statTasks")
    d json.SetAt($g(ordTasks),"ordTasks")
    s curDateTime=$zd(+$h,3)_" "_$zt($p($h,",",2))
    d json.SetAt($g(curDateTime),"curDateTime")
    //q json
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errGetOrderTasksV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100010","-1")
}

/// 手动选择病人 
/// w ##class(Nur.MNISV3.Service.Order).OnSelectPatientV4("OrderExecute","select","[{""barcode"":""5"",""entity"":"""",""manual"":"""",""result"":"""",""scan"":""""}]","{""single"":""1"",""select"":""0"",""activity"":""UniversalActivity"",""scan"":""0"",""multiple"":""0"",""sheet"":""[DefaultSee, DefaultSee]"",""check"":""0"",""manual"":""1"",""panel"":"""",""open"":""0"",""switch"":""0"",""status"":""""}","","{""id"":""ll_patselect"",""value"":""1217""}","8","23","3","20665","2")
/// "motion": "select" 是选择患者
/// "extras": "{\"single\":\"1\",\"select\":\"0\",\"activity\":\"UniversalActivity\",\"scan\":\"0\",\"multiple\":\"0\",\"sheet\":\"[DefaultSee, DefaultSee]\",\"check\":\"0\",\"manual\":\"1\",\"panel\":\"\",\"open\":\"0\",\"switch\":\"0\",\"status\":\"\"}",
/// "element": "{\"id\":\"ll_patselect\",\"value\":\"1217\"}",
/// entities 实体:  "barcode": "0000000598" 登记号 或瓶贴 ; "entity": "patient" 患者 或医嘱order
///               "manual": "0" 是否手动 "scan": "1" 是否扫码 ; "result": "1708" 结果是就诊号
/// behavior 行为: "request" 是请求; "open"或"close"是打开或关闭panels面板 "check"或"uncheck" 是由onCheckOrderV4发起
/// request  请求名称 "GetOrdersV4" 或"GetOrderTasksV4" 或"GetOrdersV4&GetOrderTasksV4" 或"ExecOrSeeOrderV4"
/// dialog   对话框  格式固定看CreateDialog方法
/// toast    通用toast提示
ClassMethod OnSelectPatientV4(module, motion, entities, extras, callback, element, locId, groupId, wardId, userId, hospitalId) As %String
{
    n (module, motion, entities, extras,callback,element,locId, groupId, wardId,userId,hospitalId) 
    s $zt="errOnSelectPatientV4"
    s ^GRS("OnSelectPatientV4")=$lb(module, motion, entities, extras,callback,element,locId, groupId, wardId,userId,hospitalId) 
    s msg="",msgcode="999999",status="0" //定义初始值
    s elementObj=##class(Nur.MNISV3.Common.JSON).Decode(element) 
    s eleId=elementObj.GetAt("id")
    s eleValue=elementObj.GetAt("value")
    // 选择患者
    i eleId="ll_patselect" { 
        s episodeId=eleValue //是就诊号
        w episodeId,!
        s regNo=##class(Nur.NIS.Service.Base.Patient).GetRegNo(episodeId)
        s name=##class(Nur.NIS.Service.Base.Patient).GetName(episodeId)
        s sex=##class(Nur.NIS.Service.Base.Patient).GetSex(episodeId)
        s bedCode=##class(Nur.NIS.Service.Base.Patient).GetBedCode(episodeId)
        s age=##class(Nur.NIS.Service.Base.Patient).GetAge(episodeId)
        s newEntities=##class(%ListOfDataTypes).%New() 
        s obj=..CreateObject("barcode,entity,manual,result,scan",regNo,"patient","1",episodeId,"0")
        d newEntities.Insert(obj)
        //dialog不为空，则患者信息弹框
        s message="<h2>"_bedCode_"  "_name_"</h2><br>"_sex_"  "_age_"  "_regNo 
        s behavior="request" // "request" 是请求
        s request="GetOrdersV4&GetOrderTasksV4" //切换患者 重置刷新
        s dialog= ..CreateDialog("提示",message,"确定",behavior,request,"取消","cancel")
    }
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(newEntities),"entities")
    d json.SetAt($g(dialog),"dialog")
    //这里也可以返回，就直接刷新了，dialog弹框点击按钮才刷新
    //d json.SetAt($g(behavior),"behavior")
    //d json.SetAt($g(request),"request")
    s curDateTime=$zd(+$h,3)_" "_$zt($p($h,",",2))
    d json.SetAt($g(curDateTime),"curDateTime")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errOnSelectPatientV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100010","-1")
}

/// 手动选择医嘱 
/// "motion": "check"或"uncheck" 选择医嘱
/// "extras": "{\"single\":\"1\",\"select\":\"0\",\"activity\":\"UniversalActivity\",\"scan\":\"0\",\"multiple\":\"0\",\"sheet\":\"[DefaultSee, DefaultSee]\",\"check\":\"0\",\"manual\":\"1\",\"panel\":\"\",\"open\":\"0\",\"switch\":\"0\",\"status\":\"\"}",
/// "element": "{\"id\":\"ll_ord_child\",\"value\":\"669||1799\"}",
/// w ##class(Nur.MNISV3.Service.Order).OnCheckOrderV4("OrderExecute","check","","{""single"":""1"",""select"":""0"",""activity"":""UniversalActivity"",""scan"":""0"",""multiple"":""0"",""sheet"":""[DefaultSee, DefaultSee]"",""check"":""0"",""manual"":""1"",""panel"":"""",""open"":""0"",""switch"":""0"",""status"":""""}","","{""id"":""ll_ord_child"",""value"":""669||1799""}","8","23","3","20665","2")
/// w ##class(Nur.MNISV3.Service.Order).OnCheckOrderV4("OrderExecute","check","[{""barcode"":""1174||1169||145"",""entity"":""order"",""manual"":""1"",""result"":""1217"",""scan"":""0""}]","{""single"":""1"",""select"":""0"",""activity"":""UniversalActivity"",""scan"":""0"",""multiple"":""0"",""sheet"":""[CQSYD, CQSYD]"",""check"":""0"",""manual"":""1"",""panel"":"""",""open"":""0"",""switch"":""0"",""status"":""""}","","{""id"":""ll_ord_child"",""value"":""1174||1169||146""}","8","23","3","20665","2")
ClassMethod OnCheckOrderV4(module, motion, entities, extras, callback, element, locId, groupId, wardId, userId, hospitalId) As %String
{
    n (module, motion, entities, extras,callback,element,locId, groupId, wardId,userId,hospitalId,%session)  // 必须加%session 调用别人代码会用到session
    s $zt="errOnCheckOrderV4"
    s ^GRS("OnCheckOrderV4")=$lb(module, motion, entities, extras,callback,element,locId, groupId, wardId,userId,hospitalId) 
    s msg="",msgcode="999999",status="0" //定义初始值
    s extrasObj=##class(Nur.MNISV3.Common.JSON).Decode(extras) 
    s elementObj=##class(Nur.MNISV3.Common.JSON).Decode(element) 
    s eleId=elementObj.GetAt("id")
    s eleValue=elementObj.GetAt("value")
    //"sheet":"[DefaultSee, DefaultSee]" 单据code最多2个
    s sheetStr=extrasObj.GetAt("sheet")
    s curSheetCode=""
    i sheetStr'="" {
        s sheetsList=##class(Nur.MNISV3.Common.JSON).Decode(sheetStr) 
        s curSheetCode=sheetsList.GetAt(1)
    }
    s eleValue=$replace(eleValue,"-","||")
    //w curSheetCode,"-",eleValue,!
     //先处理entities-空 创建一个entities
    i entities="" s newEntities=##class(%ListOfDataTypes).%New()
    else  s newEntities=##class(Nur.MNISV3.Common.JSON).Decode(entities)
    
    s buttons=##class(%ListOfDataTypes).%New() 
    s execFlag="false" //执行状态标志
    // 选择医嘱
    i eleId="ll_ord_child" { //多个用^分割
       f idx=1:1:$l(eleValue,"^") {
            s oeoreId=$p(eleValue,"^",idx)
            s oeordID=$p(oeoreId,"||",1)
	        s oeoriSub=$p(oeoreId,"||",2)
            i $l(oeoreId,"||")=3 {
                s oeoreSub=$p(oeoreId,"||",3)
                s execStatusID=$p($g(^OEORD(oeordID,"I",oeoriSub,"X",oeoreSub)),"^",16) //执行状态
                i execStatusID'="" s execStatusCode=$p($g(^OEC("STAT",execStatusID)),"^",1)  
                //"F^已执行^执^#7B7B7B" "D^停止^停^#0080FF" "C^撤销执行^未^#02C874
                i $g(execStatusCode)="F" s execFlag="true" 
            }
            // 先移除重复的
            s newEntities=##class(Nur.MNISV3.Common.Utils).RemoveEntitiesByBarCode(newEntities,oeoreId)
            i motion="check" { //添加
                s obj=..CreateObject("barcode,entity,manual,result,scan",oeoreId,"order","1",+^OEORD(+oeoreId),"0")
                d newEntities.Insert(obj)
            }
            //检查状态
            i curSheetCode'="DefaultSee" {
                //s status=..CheckOrderStatus(oeordID,oeoriSub,oeoreSub,curSheetCode)
                //w status,!
                //q:(status="0") //状态不对，不处理
            }
       }
    }
    s buttons=##class(%ListOfDataTypes).%New() 
    s sheetButtons=##class(Nur.NIS.Service.OrderExcute.QueryOrder).GetSheetButtons(hospitalId,curSheetCode,execFlag)
    w $isobject(sheetButtons)_"-"_newEntities.Count(),!
    b ;111
    // 必须有医嘱
    i ('$isobject(sheetButtons))&&(newEntities.Count()>0) {
        s sheetButtons=##class(Nur.MNIS.Common.JSON).Decode(sheetButtons)
        f i=1:1:sheetButtons.Count() {
            s sheetButton=sheetButtons.GetAt(i)
            s code=sheetButton.GetAt("code")
            //q:(sheetCode="PSD")&&(code="excuteOrder")
            s desc=sheetButton.GetAt("desc")
            //"执行"="excuteOrder" "撤销执行"="cancelOrder" "处理医嘱"="seeOrder" "撤销处理"="cancelSeeOrder" "皮试结果"="setSkinTest" "停止执行"="stopExcuteOrder"
            s keys="id,text,behavior,request,backgroundColor,color,disabled" //'确定'按钮要唯一Id 会传参给后台button
            i code="seeOrder" { // 处理医嘱- "A|接受^S|完成^R|拒绝"
                d buttons.Insert(##class(Nur.Tools).RapidAssembly(keys,code_"^A","接受","","","#067FFA"))
                d buttons.Insert(##class(Nur.Tools).RapidAssembly(keys,code_"^S","完成","","","#0F620C"))
                d buttons.Insert(##class(Nur.Tools).RapidAssembly(keys,code_"^R","拒绝","","","#A41E0F"))
            }
            i code="excuteOrder" { // 执行医嘱- "F"
                d buttons.Insert(##class(Nur.Tools).RapidAssembly(keys,code,"执行医嘱","","","#067FFA"))
            }
            i code="setSkinTest" { // 皮试结果- "F"
                d buttons.Insert(##class(Nur.Tools).RapidAssembly(keys,code,"皮试结果","","","#006FD2"))
            }
            //i code="skinTime" s execode="ST",hasSkinTime=1
            //i code="stopExcuteOrder" s execode="SE"
        }
    }
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(newEntities),"entities")
    //d json.SetAt($g(dialog),"dialog")
    d json.SetAt($g(motion),"behavior") //即motion用来勾选医嘱刷新adapter
    d json.SetAt($g(buttons),"buttons") // 必须返回按钮 针对不同sheetCode
    d json.SetAt($g(curSheetCode),"curSheetCode")
    d json.SetAt($g(sheetButtons),"sheetButtons")
    s curDateTime=$zd(+$h,3)_" "_$zt($p($h,",",2))
    d json.SetAt($g(curDateTime),"curDateTime")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errOnCheckOrderV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100010","-1")
}

/// 查询医嘱详情
/// w ##class(Nur.MNISV3.Service.Order).GetOrderDetailV4("","","669||25||801","CQZSD","","2")
ClassMethod GetOrderDetailV4(module, motion, element, sheetCode, extras, hospitalId) As %String
{
    n (module,motion,element,sheetCode,extras,hospitalId,%session)
    s $zt="errGetOrderDetailV4"
     s msg="",msgcode="999999",status="0" //定义初始值
    s ^DHC("errGetOrderDetailV4")=$lb(module,motion,element,sheetCode,extras,hospitalId)
    s oeoreId=element
    s episodeId=+^OEORD(+oeoreId)
    s patient=##class(Nur.MNISV3.Service.Patient).GetBasicInfo("",episodeId)
    s ordInfo=..GetBasicInfo(oeoreId,"")
    s ordChildInfo=##class(%ListOfDataTypes).%New()
    s maiOrdID=$p(ordInfo,"||",1,2)
    s child=0
    f
    {
       s child=$O(^OEORDi(0,"OEORI",oeoreId,maiOrdID,child))
       q:(child="")
       s ordChildArr=..GetBasicInfo(+oeoreId_"||"_child_"||"_$p(oeoreId,"||",3),"")
       d ordChildInfo.Insert(ordChildArr)
       
    }
    d ordInfo.SetAt(ordChildInfo,"children")
    s values=##class(%ListOfDataTypes).%New()
    s columns=##class(Nur.MNISV3.Service.QueryOrder).GetSheetColumns(sheetCode,hospitalId)
    s colLen=columns.Count()
    f colInd=1:1:colLen {
        s column=columns.GetAt(colInd)
        s key=column.GetAt("key")
        s title=column.GetAt("title")
        s val=..GetBasicFieldValue(key,oeoreId)
        s value=##class(%ArrayOfDataTypes).%New()
        d value.SetAt(key,"key")
        d value.SetAt(title,"title")
        d value.SetAt(val,"value")
        d values.Insert(value)
    }
    s flow=##class(%ArrayOfDataTypes).%New()


    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(patient),"patInfo")
    d json.SetAt($g(ordInfo),"ordInfo")
    d json.SetAt($g(values),"columns")
    d json.SetAt($g(flow),"OCLFlow")
    //q json
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errGetOrderDetailV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100010","-1")
}

/// 医嘱闭环配置
ClassMethod GetOrderConfigV4(locId, groupId, wardId) As %String
{
    n (locId,groupId,wardId)
    s $zt="errGetOrderConfigV4"
    s ^GRS("GetOrderConfigV4")=$lb(locId,groupId,wardId)
    s msg="",msgcode="999999",status="0" //定义初始值
    s configFlags=##class(%ArrayOfDataTypes).%New()
    d configFlags.SetAt($g(vitalSignLeftFilterFlag),"vitalSignLeftFilterFlag")
    //是否病人选择
    s patSelectFlag="1"
    d configFlags.SetAt(patSelectFlag,"patSelectFlag")
    //默认单选：点击病人并关闭
    s patDialogFlag="1"
    d configFlags.SetAt(patDialogFlag,"patDialogFlag")
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    //d json.SetAt("2","scantimes")
    d json.SetAt($g(status),"status")
    //d json.SetAt($g(screenParts),"screenParts")
    //d json.SetAt($g(stdatetime),"schStDateTime")  
    //d json.SetAt($g(enddatetime),"schEnDateTime")
    s curDateTime=$zd(+$h,3)_" "_$zt($p($h,",",2))
    d json.SetAt($g(curDateTime),"curDateTime")
    d json.SetAt(configFlags,"configFlags")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errGetOrderConfigV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100010","-1")
}

/// 任务总览
ClassMethod GetOrdersBySheetCode(episodeIDStr, currentSheetCode, groupID, startDate, startTime, endDate, endTime, hospID, wardID, locID, doctorOrderFlag = "true", createOrderFlag = "false", orderType = "A", excutedOrderFlag = "false", ifPrint = "", longOrderFlag = "false", tempOrderFlag = "false", newCreateFlag = "false", newDisconFlag = "false", confirmFlag = "") As %ArrayOfDataTypes
{
     ///默认参数
    s PDAParam=##class(OrderParam).%New()
    s PDAParam.PDAStart=1 ;(pageNo-1)*limitNo+1
    s PDAParam.PDALimit= 999999 ;pageNo*limitNo
    s PDAParam.PDAScreenings=screenParts
    s PDAParam.PDAMulti=0
    q ##class(Nur.MNISV3.Service.QueryOrder).GetOrders(PDAParam,episodeIDStr, currentSheetCode, groupID, startDate, startTime, endDate, endTime, hospID, wardID, locID, doctorOrderFlag, createOrderFlag, orderType, excutedOrderFlag, ifPrint, longOrderFlag, tempOrderFlag, newCreateFlag, newDisconFlag, confirmFlag)
}

/// 将PC医嘱列表 转化PDA 患者医嘱列表
ClassMethod ConvertPdaPatOrds(sheetCode, patOrds, disposeStateInfo)
{
    n (sheetCode,patOrds,disposeStateInfo,%session) // 必须加%session 调用别人代码会用到session
    s orderInfoMain=##Class(%ListOfDataTypes).%New()
    s orders=patOrds.Data("orders")
    //优化下面代码
    f ordi=1:1:orders.Size {
        i sheetCode'="DefaultSee"{
            s execInfos=orders.Data(ordi).Data("execInfos")
            i $d(execInfos){ // 取执行记录信息
                f ordj=1:1:execInfos.Size {
                    s orderInfoArr= ..GetOrderInfoAndChildren(execInfos.Data(ordj),disposeStateInfo) //获取医嘱基本信息和子医嘱
                    d orderInfoMain.Insert(orderInfoArr)
                }
            }
        }else {
            i $d(orders.Data(ordi).Data("ID")){ 
                s orderInfoArr= ..GetOrderInfoAndChildren(orders.Data(ordi),disposeStateInfo) //获取医嘱基本信息和子医嘱
                d orderInfoMain.Insert(orderInfoArr)
            }
        }
    }
    q orderInfoMain
}

/// 获取医嘱基本信息和子医嘱
ClassMethod GetOrderInfoAndChildren(ordObj, disposeStateInfo)
{
    n (ordObj,disposeStateInfo,%session)
    s mainId=ordObj.Data("ID")
    s disposeStatCode=ordObj.Data("disposeStatCode")
    // "childs": ["669||77||850" ] 或 "childs":[{"ID":"9||94","labNo":""}
    s childs=ordObj.Data("childs")  
    // 第三个参数为customStyle自定义样式
    s orderInfo=..GetBasicInfo(mainId,"","customStyle")
    s customStyleInfo1=orderInfo.GetAt("customStyleInfo")
    i customStyleInfo1="" s customStyleInfo1=##class(%ArrayOfDataTypes).%New()
    s checkbox="1",allowManualCheck="1"
    s seeOrderUserdr=$p($g(^OEORD(+mainId,"I",$p(mainId,"||",2),"NUR")),"^",4) //处理人
    s buttons=##class(%ListOfDataTypes).%New()
    s slide=##class(%ArrayOfDataTypes).%New()
    d slide.SetAt(buttons,"buttons")
    //侧滑 剩余 CallbackBean callback;String motion; String autoClickTimeout; String autoHideOnClick;
    s keys="id,text,behavior,request,backgroundColor,color,disabled" //'确定'按钮要唯一Id 会传参给后台button
    i $l(mainId,"||")=3 { //"撤销执行","cancelOrder"
        s execStatusID=$p($g(^OEORD(+mainId,"I",$p(mainId,"||",2),"X",$p(mainId,"||",3))),"^",16) //执行状态
        i execStatusID'="" s execStatusCode=$p($g(^OEC("STAT",execStatusID)),"^",1)  
        //"F^已执行^执^#7B7B7B" "D^停止^停^#0080FF" "C^撤销执行^未^#02C874
        i ($g(execStatusCode)="F")||($g(execStatusCode)="D") {
            s checkbox="",allowManualCheck="" //不能勾选
            i $g(execStatusCode)="F" {
                d buttons.Insert(##class(Nur.Tools).RapidAssembly(keys,"cancelOrder","撤销执行","","","#A41E0F"))
                d orderInfo.SetAt(slide,"slide")
            }
        }
    }else {
        i seeOrderUserdr'=""{ //"撤销处理","cancelSeeOrder",
        s checkbox="",allowManualCheck="" //不能勾选
        d buttons.Insert(##class(Nur.Tools).RapidAssembly(keys,"cancelSeeOrder","撤销处理","","","#A41E0F"))
        d orderInfo.SetAt(slide,"slide")
    } 
    }
    //是否显示勾选 
    d orderInfo.SetAt(checkbox,"checkbox")
    //是否手动勾选 ;
    d orderInfo.SetAt(allowManualCheck,"allowManualCheck")
    //时间分组 codeFilter;
    
    s orderInfoChild=##Class(%ListOfDataTypes).%New()
    //w childs.Count(),!
    f ordx=1:1:childs.Count() {
        i $isobject(childs.GetAt(ordx)){ //DefaultSee 子医嘱是{"ID":"9||94","labNo":""}
            s childId=childs.GetAt(ordx).GetAt("ID")
        }else {
            s childId=childs.GetAt(ordx)
        }
        s orderInfoChildArr=##Class(%ArrayOfDataTypes).%New()
        i childId["||" {
            s orderInfoChildArr=..GetBasicInfo(childId,orderInfoChildArr,"customStyle")
            s customStyleInfo2=orderInfoChildArr.GetAt("customStyleInfo")
            i customStyleInfo2'="" { //合并customStyleInfo1 和 customStyleInfo2   
                s allCustom=customStyleInfo1.GetAt("custom") ,tmCustom=customStyleInfo2.GetAt("custom")
                i allCustom'[tmCustom d customStyleInfo1.SetAt(allCustom_tmCustom,"custom") //防止重复
                d customStyleInfo1.SetAt(customStyleInfo1.GetAt("custom1")_customStyleInfo2.GetAt("custom1"),"custom1")
                d customStyleInfo1.SetAt(customStyleInfo1.GetAt("custom2")_customStyleInfo2.GetAt("custom2"),"custom2")
                d customStyleInfo1.SetAt(customStyleInfo1.GetAt("custom3")_customStyleInfo2.GetAt("custom3"),"custom3")
            }
        }
        d orderInfoChild.Insert(orderInfoChildArr)
    }
    d orderInfo.SetAt(orderInfoChild,"children")
    d orderInfo.SetAt(customStyleInfo1,"customStyleInfo")
    i $g(disposeStateInfo)'="" { //处置状态信息
         s obj=disposeStateInfo.GetAt(disposeStatCode)
         i $isobject(obj) {
            d orderInfo.SetAt(obj.GetAt("desc"),"disposeStatDesc")
            d orderInfo.SetAt(obj.GetAt("color"),"disposeStatColor")
         }
    }
    q orderInfo
}

/// 医嘱基本信息--ordInfo对象不传会创建并返回，type是类型对应处理
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Order).GetBasicInfo("144||149||344"))  
ClassMethod GetBasicInfo(oeoreId, ordInfo = "", type = "")
{
    n (oeoreId,ordInfo,type,%session)
    s $zt="errGetBasicInfo" // 必须加%session 调用别人代码会用到session
    s oeoreId=$replace(oeoreId,"-","||")
    s oeordID=$p(oeoreId,"||",1)
	s oeoriSub=$p(oeoreId,"||",2)
    s oeoreSub=$p(oeoreId,"||",3)
    i (ordInfo="")||('$isobject(ordInfo)) s ordInfo=##class(%ArrayOfDataTypes).%New()
    i oeoreId="" q $g(ordInfo)
    d ordInfo.SetAt(..GetBasicFieldValue("ID",oeoreId),"ID")  
    d ordInfo.SetAt(..GetBasicFieldValue("bedCode",oeoreId),"bedCode")
    d ordInfo.SetAt(..GetBasicFieldValue("regNo",oeoreId),"regNo")
    d ordInfo.SetAt(..GetBasicFieldValue("patName",oeoreId),"patName")
    d ordInfo.SetAt(..GetBasicFieldValue("arcimDesc",oeoreId),"arcimDesc") //医嘱名称
    d ordInfo.SetAt(..GetBasicFieldValue("createDateTime",oeoreId),"createDateTime") //开医嘱时间
    d ordInfo.SetAt(..GetBasicFieldValue("ctcpDesc",oeoreId),"ctcpDesc") //医生
    d ordInfo.SetAt(..GetBasicFieldValue("phcinDesc",oeoreId),"phcinDesc") //用药途径
    d ordInfo.SetAt(..GetBasicFieldValue("phcfrCode",oeoreId),"phcfrCode") //频率
    d ordInfo.SetAt(..GetBasicFieldValue("oecprDesc",oeoreId),"oecprDesc") //优先级
    d ordInfo.SetAt(..GetBasicFieldValue("ordStatDesc",oeoreId),"ordStatDesc") //医嘱状态
    d ordInfo.SetAt(..GetBasicFieldValue("doseQtyUnit",oeoreId),"doseQtyUnit")  //剂量
    d ordInfo.SetAt(..GetBasicFieldValue("speedFlowRate",oeoreId),"speedFlowRate")
    d ordInfo.SetAt(..GetBasicFieldValue("speedFlowUnit",oeoreId),"speedFlowUnit")
    d ordInfo.SetAt(..GetBasicFieldValue("sttDateTime",oeoreId),"sttDateTime") //要求执行时间
    d ordInfo.SetAt(..GetBasicFieldValue("disposeStatCode",oeoreId),"disposeStatCode") //处置状态码
    d ordInfo.SetAt(..GetBasicFieldValue("labNo",oeoreId),"labNo")
    d ordInfo.SetAt(..GetBasicFieldValue("notes",oeoreId),"notes")
    s execDateTime=##class(Nur.NIS.Service.OrderExcute.OrderInfo).GetExecDateTime(oeoreId)  //执行时间
    s execCtcpDesc=##class(Nur.NIS.Service.OrderExcute.OrderInfo).GetExecCtcpDesc(oeoreId)   //执行人
    s abnorm=##class(Nur.NIS.Service.OrderExcute.OrderInfo).GetSkinTestAbnormal(oeoreId)
    s JPFlag=##class(Nur.NIS.Service.OrderExcute.OrderInfo).GetOrdJFFlag(oeoreId)
    s highRiskFlag=##class(Nur.NIS.Service.OrderExcute.OrderInfo).GetOrdHighRiskFlag(oeoreId)
    s orderStage=##class(Nur.NIS.Service.OrderExcute.OrderInfo).GetOrdStage(oeoreId)
    s notifyClinician=##class(Nur.NIS.Service.OrderExcute.OrderInfo).GetNotifyClinician(oeoreId) //加急标记
    s specDesc=##class(Nur.NIS.Service.OrderExcute.OrderInfo).GetSpecDesc(oeoreId) //标本名称 "血清"
    s tubeColor=##class(Nur.NIS.Service.OrderExcute.OrderInfo).GetTubeColor(oeoreId) //管颜色 "Purple"
    s tubeColorDesc=##class(Nur.NIS.Service.OrderExcute.OrderInfo).GetTubeColorDesc(oeoreId) //管颜色描述 "紫色管"
    //双签标识
    s doubleFlag=##class(Nur.NIS.Service.OrderExcute.QueryOrder).IsDoubleSignOrderGroup(oeordID,oeoriSub,hospID)="Y"
    d ordInfo.SetAt(doubleFlag,"doubleFlag")
    d ordInfo.SetAt(abnorm,"abnorm")
    d ordInfo.SetAt(JPFlag,"JPFlag")
    d ordInfo.SetAt(highRiskFlag,"highRiskFlag")
    d ordInfo.SetAt(orderStage,"orderStage")
    // 支持 item1 ~ item10 自定义
    // highRiskFlag "高危"  abnorm "Y"或(+) 即阳性   "N"或(-) 即阴性
    // filteFlagExtend "静配" oecprDesc  "取药医嘱"  orderStage "术中"
    //d ordInfo.SetAt("item1","item1")
    //specDesc   tubeColor "Purple" tubeColorDesc "紫色管"

    /*自定义样式--先测试用
       1，圆角标签(白色字体+蓝色背景)： <custom color='#FFFFFF' background='#4C95EF' radius='5'>摆药</custom>
       2，宽度100的透明块(增加边距)：<custom width='20'>透明块</custom>
       3，APP本地图标（src对应名称）<custom src='ic_v4_tag_yang'>图片</custom>
       4，普通html标签：<span style='color:#FF0000'>网内图标</span>
          复杂样式: <custom color='#00ff00' size='40px' weight='italic'>包内图标</custom>
    */
    i $g(type)="customStyle" {
        s custom="" // custom 是item 头部第一行自定样式
        i $g(JPFlag)'=""  s custom=$g(custom)_"<span style='color:#00ff00'>"_JPFlag_"</span>"
        i $g(orderStage)'=""  s custom=$g(custom)_"<span style='color:#3DA4E0'>"_orderStage_"</span>"
        i $g(highRiskFlag)'=""   s custom=$g(custom)_"<span style='color:#FF0000'>"_highRiskFlag_"</span>"
        i $g(notifyClinician)="Y"   s custom=$g(custom)_"<custom color='#FF0000' size='30px' weight='bold' >加急</custom>"
        i $g(abnorm)'="" {
            i $g(abnorm)["+" s custom=$g(custom)_"<custom width='80'>透明块</custom><custom src='ic_v4_tag_yang'>图</custom>"
            i $g(abnorm)["-" s custom=$g(custom)_"<custom width='80'>透明块</custom><custom src='ic_v4_tag_yin'>图</custom>"
        }
        s custom1=""
        i $g(specDesc)'="" s custom1=$g(custom1)_"<span style='color:"_tubeColor_"'>"_specDesc_"   "_tubeColorDesc_"</span>"
        //s custom2="<custom color='#00ff00' size='40px' weight='bold'>"_c2_"</custom>"
        //执行人 执行时间
        i $g(execCtcpDesc)'="" s custom3="<custom color='#FFFFFF' background='#4C95EF' radius='10'>执行</custom>   "_execDateTime_"  "_execCtcpDesc
        s customStyleInfo=##class(%ArrayOfDataTypes).%New()
        d customStyleInfo.SetAt("#FFFFFF","backgroundColor") //背景颜色
        d customStyleInfo.SetAt($g(custom),"custom") // 第一行 标签右边
        d customStyleInfo.SetAt($g(custom1),"custom1") //医嘱下面第一行
        d customStyleInfo.SetAt($g(custom2),"custom2") //医嘱下面第二行
        d customStyleInfo.SetAt($g(custom3),"custom3") //医嘱下面第三行
        i ($g(custom)'="")||($g(custom1)'="")||($g(custom2)'="")||($g(custom3)'="") {
            d ordInfo.SetAt(customStyleInfo,"customStyleInfo") // 不为空才设置
        }
    }
    q $g(ordInfo)
errGetBasicInfo
    w $ze,!
    q $g(ordInfo)
}

/// 处理筛选
ClassMethod handleScreenParts(screenParts, excutedOrderFlag)
{
    n (screenParts,excutedOrderFlag)
    i screenParts'="" {
        s screenLen=$l(screenParts,"^")
        f i=1:1:screenLen {
            s screenPart=$p(screenParts,"^",i)
            s screenKey=$p(screenPart,"|",1)
            s screenValue=$p(screenPart,"|",2)
            s tmpScreen(screenKey)=screenValue
        }
    }
    i $d(tmpScreen("execFlag")) {
        i tmpScreen("execFlag")["已执行" s excutedOrderFlag="true"
        i tmpScreen("execFlag")["未执行" s excutedOrderFlag="false"
    }
}

/// 获取左侧单据列表
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Order).GetSheetsListOfSSGroup("23","2","8"))
ClassMethod GetSheetsListOfSSGroup(groupID, hospitalId = "", locId = "") As %ListOfDataTypes
{
    n (groupID,hospitalId,locId,%session)
    s ^GRS("GetSheetsListOfSSGroup")=$lb(groupID,hospitalId,locId)
    i (hospitalId="")&&($d(%session)) s hospitalId=%session.Get("LOGON.HOSPID")
    i (locId="")&&($d(%session)) s locId=%session.Get("LOGON.CTLOCID")
    s sheetsList=##class(%ListOfDataTypes).%New()
    //调用PC的
    s sheetListStr=##class(Nur.NIS.Service.OrderExcute.SheetConfig).GetCurrentSheets(groupID,locId)
    s sheetListObj=##class(Nur.MNISV3.Common.JSON).Decode(sheetListStr)
    //是否急诊护士站 "emergencyFlag": true,
    f idx=1:1:sheetListObj.Count() {
        s obj=sheetListObj.GetAt(idx)
        s desc=$replace(obj.GetAt("desc"),"*","")
        s sheetSetting=##class(%ArrayOfDataTypes).%New()
        d sheetSetting.SetAt(obj.GetAt("code"),"sheetCode")
        d sheetSetting.SetAt(desc,"sheetName")
        d sheetsList.Insert(sheetSetting)
    }
    q sheetsList
}

/// 默认单据code
ClassMethod GetDefaultSheetStr(groupID, hospitalId = "", locId = "") As %String
{
    n (groupID,hospitalId,locId,%session)
    s ^GRS("GetDefaultSheetStr")=$lb(groupID,hospitalId,locId)
    s mainHospID=##class(web.DHCBL.BDP.BDPMappingHOSP).GetDefHospIdByTableName("Nur_IP_ExecuteGroup",hospitalId,"")
    // 获取安全组配置
    s applyBusiness="NurE"
    s byGroupId=$o(^CF.NUR.NIS.ExecuteGroupI("TypeItem",mainHospID," 1"," "_groupID," "_$zcvt(applyBusiness,"U"), ""))
    i (byGroupId="") s byGroupId=$o(^CF.NUR.NIS.ExecuteGroupI("TypeItem",mainHospID," 1"," "_groupID, ""))
    // 获取科室配置
    s byLocId=$o(^CF.NUR.NIS.ExecuteGroupI("TypeItem",mainHospID," 2"," "_locId," "_$zcvt(applyBusiness,"U"), ""))
    i (byLocId="") s byLocId=$o(^CF.NUR.NIS.ExecuteGroupI("TypeItem",mainHospID," 2"," "_locId, ""))
    // 如果科室配置不为空则取科室配置
    i byLocId'="" s byGroupId=byLocId
    i (byGroupId'="") d
    .s byGroupSubId="" f  s byGroupSubId=$o(^CF.NUR.NIS.ExecuteGroupSubD(byGroupId,byGroupSubId)) q:(byGroupSubId="")  d
    ..s data=$g(^CF.NUR.NIS.ExecuteGroupSubD(byGroupId,byGroupSubId))
    ..s sheetId=$lg(data,2)
    ..s default=$lg(data,3)
    ..q:(default'="Y")
    ..s obj=##class(CF.NUR.NIS.NurseSheetSet).%OpenId(sheetId)
    ..q:('$isobject(obj))
    ..s code=obj.NSSCode
    ..d obj.%Close()
    ..s defaultSheetStr=hospitalId_"@"_code
    q $g(defaultSheetStr)
}

/// 通过扫码执行记录ID 获取医嘱信息  
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Order).GetOrderInfoByBarCode("669||76||822"))
ClassMethod GetOrderInfoByBarCode(barcode)
{
    n (barcode,%session)
    s ^GRS("GetOrderInfoByBarCode")=$lb(barcode)
    s OrderList=##class(%ListOfDataTypes).%New()
    s barcode=$replace(barcode,"||","-")
    s ord=+barcode
    s chl=$P(barcode,"-",2)
    s sub=$P(barcode,"-",3)
    s orderInfo=..GetBasicInfo(barcode)
    d OrderList.Insert(orderInfo)
    s childOrdInfo=##class(%ListOfDataTypes).%New()
    s mainOeoreID=ord_"||"_chl_"||"_sub
    s child=0  
    f 
    {   s child=$o(^OEORDi(0,"OEORE",mainOeoreID,ord,child))
        q:child=""
        s childSub=0  
        f 
        {   s childSub=$o(^OEORDi(0,"OEORE",mainOeoreID,ord,child,childSub))
            q:childSub=""
            s childOrdId=ord_"||"_child_"||"_childSub
            s arcimDr=$P($G(^OEORD(ord,"I",child,1)),"^",2)
            s itemCatDr=$P($G(^ARCIM(+arcimDr,$p(arcimDr,"||",2),1)),"^",10)
            s ordTyp=$P(^ARC("IC",itemCatDr),"^",7)
            continue:ordTyp'="R" //过滤掉非药品医嘱 
            s childorderInfo=..GetBasicInfo(ord_"||"_child_"||"_childSub)
            d OrderList.Insert(childorderInfo)
        }
    }
    q OrderList
}

/// 对应PC的配置：CT.NUR.NIS.NurseBasicData
/// 数据类型 NBDType  患者信息:0 ,医嘱信息:1 ,执行记录信息:2 ,打印信息:3 ,护理计划:4 ,分娩信息:5
/// 索引：^CT.NUR.NIS.NurseBasicDataI("Code"," NBDType"," NBDCode")
/// w ##class(Nur.MNISV3.Service.Order).GetBasicFieldValue("arcimDesc","1174||1169||111")
ClassMethod GetBasicFieldValue(key, oeoreId) As %String
{
    n (key,oeoreId,%session)
    i (key="ID") {
        s value=oeoreId
    } else {
        s len=$l(oeoreId,"||")
        i (len=3) { ;执行记录信息
            s id=$o(^CT.NUR.NIS.NurseBasicDataI("Code"," 2"," "_$zcvt(key,"U"),""))
        } elseif (len=2) { ;医嘱信息
            q:(key="sttDateTime") ..GetSttDateTimeAsStr(oeoreId)
        }
        i ($g(id)="") s id=$o(^CT.NUR.NIS.NurseBasicDataI("Code"," 1"," "_$zcvt(key,"U"),""))
        q:($g(id)="") ""
        s type=$lg($g(^CT.NUR.NIS.NurseBasicDataD(id)),3)
        s value=..GetBasicDataByCode(type,key,oeoreId)
    }
    q $g(value)
}

/// 通过code获取基础数据值
/// 基础数据表code,就诊号,医嘱id,执行记录id
/// w ##class(Nur.MNISV3.Service.Order).GetBasicDataByCode(1,"arcimDesc","1174||1169||111")
ClassMethod GetBasicDataByCode(type, code, parameter = "") As %String
{
    n (type, code, parameter,%session)
    q:code="" ""
    s code=$zcvt(code,"U")
    s rowId=$o(^CT.NUR.NIS.NurseBasicDataI("Code"," "_type," "_code,""))
    q:rowId="" ""
    s basicData=$g(^CT.NUR.NIS.NurseBasicDataD(rowId))
    s callMethod=$lg(basicData,6)
    s className=$p(callMethod,":",1)
    s methodName=$p(callMethod,":",2)
    i (className'="")&&(methodName'="") {
        q:'##class(websys.Conversions).IsValidMethodName(className,methodName) ""
        s x="(i,o) s o=##class("_className_")."_methodName_"("""_parameter_""")"
        x (x,"",.ret)
    }else{
        q ""
    }
    q ret
}

/// 创建一个Dialog
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Order).CreateDialog("提示","是否复制该护理问题?","确定","copy","取消","outcome"))
ClassMethod CreateDialog(mTitle = "提示", message = "", okText = "确定", okBehavior = "", okRequest = "", cancelText = "取消", cancelBehavior = "cancel")
{
    n (mTitle,message,okText,okBehavior,okRequest,cancelText,cancelBehavior)
    s title=..CreateObject("text,backgroundColor",mTitle,"") 
    s message=..CreateObject("text,backgroundColor",message,"#DBECFF") 
    s buttons=##class(%ListOfDataTypes).%New() 
    //剩余 CallbackBean callback;String motion; String autoClickTimeout; String autoHideOnClick;
    d buttons.Insert(..CreateObject("text,behavior,request,backgroundColor,color,disabled",okText,okBehavior,okRequest))
    d buttons.Insert(..CreateObject("text,behavior,backgroundColor,color,disabled",cancelText,cancelBehavior))
   
    s dialog=##class(%ArrayOfDataTypes).%New()
    d dialog.SetAt($g(message),"message") //MessageBean
    d dialog.SetAt($g(title),"title") //TitleBean
    d dialog.SetAt($g(buttons),"buttons") //List<ButtonsBean>
    q dialog
}

/// 快速创建一个对象， 第一个参数“,”分割 和后面args顺序对应
/// 测试: w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Order).CreateObject("name,age","张三","18")) 
ClassMethod CreateObject(keys, args...) As %String [ Internal ]
{
    n (keys,args)
    s obj= ##class(%ArrayOfDataTypes).%New() 
    s ks=$replace(keys,"^",",") ;替换,分割符
    ;转换成 $lb("name","age")  语法：$listfromstring() 将分隔符字符串转换为列表
    s lb=$lfs(ks,","),len=$ll(lb)
    f i=1:1:len {
        s key=$lg(lb,i)  ;w $lg(lb,i) -->依次取出 name  age
        continue:(key="")
        ; 这种格式: args=2 
        ;    args(1)="张三"
        ;    args(2)=18
        s value=$g(args(i)) 
        d obj.SetAt(value,key)
    }
    q obj
}

/// 开/下医嘱日期
ClassMethod GetOrdDate(oeoreId) As %String
{
    n (oeoreId)
    s oew=+oeoreId,chl=$p(oeoreId,"||",2)
    q $p($g(^OEORD(oew,"I",chl,3)),"^",7)
}

/// 开/下医嘱时间
ClassMethod GetOrdTime(oeoreId) As %String
{
    n (oeoreId)
    s oew=+oeoreId,chl=$p(oeoreId,"||",2)
    q $p($g(^OEORD(oew,"I",chl,1)),"^",17)
}

/// 要求执行日期
ClassMethod GetSttDate(oeoreId) As %String
{
    n (oeoreId)
    s oew=+oeoreId,chl=$p(oeoreId,"||",2),sub=$p(oeoreId,"||",3)
    i (sub'="") s sttDate=$p($g(^OEORD(oew,"I",chl,"X",sub)),"^",1)
    e  s sttDate=$p($g(^OEORD(oew,"I",chl,1)),"^",9) ;开始日期
    q sttDate
}

/// 要求执行时间
ClassMethod GetSttTime(oeoreId) As %String
{
    n (oeoreId)
    s oew=+oeoreId,chl=$p(oeoreId,"||",2),sub=$p(oeoreId,"||",3)
    i (sub'="") s sttTime=$p($g(^OEORD(oew,"I",chl,"X",sub)),"^",2)
    e  s sttTime=$p($g(^OEORD(oew,"I",chl,1)),"^",10) ;开始时间
    q sttTime
}

/// w ##class(Nur.MNISV3.Service.Order).GetSttDateTimeAsStr("1271||27")
/// w ##class(Nur.MNISV3.Service.Order).GetSttDateTimeAsStr("1174||1169||111")
ClassMethod GetSttDateTimeAsStr(oeoreId) As %String
{
    n (oeoreId)
    s sttDate=..GetSttDate(oeoreId)
    s sttTime=..GetSttTime(oeoreId)
    q $s(sttDate'="":$zd(sttDate,3)_" "_$zt(sttTime,2),1:"")
}

ClassMethod GetMainOrdId(oeoreId) As %String
{
     q $p($g(^OEORD(+oeoreId,"I",$p(oeoreId,"||",2),11)),"^",39)
}

ClassMethod GetMainOreId(oeoreId) As %String
{
    n (oeoreId)
    s oew=+oeoreId,chl=$p(oeoreId,"||",2),sub=$p(oeoreId,"||",3)
    i (sub'="") s mainOreId=$p($g(^OEORD(oew,"I",chl,"X",sub,"BILL")),"^",3)
    e  s mainOreId=$p($g(^OEORD(oew,"I",chl,11)),"^",39)
    q mainOreId
}

}
